﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for BrokerAccountDB
/// </summary>
namespace CE.User
{
    public class BrokerAccountDB
    {
        public BrokerAccountDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(BrokerAccount brokeraccount)
        {
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            Int64 _result = -1;
            string _spName = "sprocBrokerInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@CEID", brokeraccount.CEID);
            _sPara.Add("@UserId", brokeraccount.UserId);
            _sPara.Add("@Email", brokeraccount.Email);
            _sPara.Add("@AuthCode", brokeraccount.AuthCode);
            _sPara.Add("@AuthSalt", brokeraccount.AuthSalt);
            _sPara.Add("@AuthHint", brokeraccount.AuthHint);
            _sPara.Add("@UsrTypeId", brokeraccount.UserTypeId);
            _sPara.Add("@IsAcceptTC", brokeraccount.IsAcceptTC);
            _sPara.Add("@RefCEUserId", brokeraccount.RefCEUserId);
            _sPara.Add("@Status", brokeraccount.Status);
            _sPara.Add("@Info1", brokeraccount.Info1);
            _sPara.Add("@Info2", brokeraccount.Info2);
            _sPara.Add("@Info3", brokeraccount.Info3);
            _sPara.Add("@Info4", brokeraccount.Info4);
            _sPara.Add("@Info5", brokeraccount.Info5);
            _sPara.Add("@Info6", brokeraccount.Info6);
            _sPara.Add("@Info7", brokeraccount.Info7);
            _sPara.Add("@Info8", brokeraccount.Info8);
            _sPara.Add("@Info9", brokeraccount.Info9);
            _sPara.Add("@Info10", brokeraccount.Info10);
            _sPara.Add("@IsReject", brokeraccount.IsReject);
            _sPara.Add("@NInfo4", brokeraccount.NInfo4);
            _sPara.Add("@NInfo5", brokeraccount.NInfo5);
            _sPara.Add("@NInfo6", brokeraccount.NInfo6);
            _sPara.Add("@Dinfo1", brokeraccount.Dinfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public Int64 Deactivate(Int64 BrokerId, Int64 AdminId)
        {
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            Int64 _result = -1;
            string _spName = "sprocBrokerAccountDeactivatedSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@BrokerId", BrokerId);
            _sPara.Add("@AdminId", AdminId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public Int64 LeadAssignPause(Int64 BrokerId, Int64 AdminId, Int64 LeadAssignStatus)
        {
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            Int64 _result = -1;
            string _spName = "sprocBrokerLeadAssignPauseResumeStatusUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@BrokerId", BrokerId);
            _sPara.Add("@AdminId", AdminId);
            _sPara.Add("@LeadAssignStatus", LeadAssignStatus);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public Int64 LeadAssignResume(Int64 BrokerId, Int64 AdminId, Int64 LeadAssignStatus)
        {
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            Int64 _result = -1;
            string _spName = "sprocBrokerLeadAssignPauseResumeStatusUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@BrokerId", BrokerId);
            _sPara.Add("@AdminId", AdminId);
            _sPara.Add("@LeadAssignStatus", LeadAssignStatus);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public BrokerAccount GetAccount(Int64 CEID)
        {
            BrokerAccount _brokeraccount = null;
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "sprocBrokerSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CEID", CEID);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _brokeraccount = new BrokerAccount();
                    _brokeraccount = FillDataRecord(_dt);
                }
            }
            return _brokeraccount;
        }

        public bool IsAlreadyExist(string Email, int UserTypeId)
        {
            bool _result = false;
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "sprocBrokerSelectSingleItemByEmail";

            SortedList _spara = new SortedList();
            _spara.Add("@Email", Email);
            _spara.Add("@Status", true);
            _spara.Add("@UserTypeId", UserTypeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _result = true;
                }
            }
            return _result;
        }

        private BrokerAccount FillDataRecord(DataTable myDataTable)
        {
            BrokerAccount myAccount = new BrokerAccount();
            myAccount.CEID = Convert.ToInt64(myDataTable.Rows[0]["CEID"].ToString());
            myAccount.UserId = myDataTable.Rows[0]["UserId"].ToString();
            myAccount.Email = myDataTable.Rows[0]["Email"].ToString();
            myAccount.AuthCode = myDataTable.Rows[0]["AuthCode"].ToString();
            myAccount.AuthSalt = myDataTable.Rows[0]["AuthSalt"].ToString();
            myAccount.UserTypeId = Convert.ToInt32(myDataTable.Rows[0]["UsrTypeId"].ToString());
            myAccount.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myAccount.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            myAccount.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            myAccount.Info2 = myDataTable.Rows[0]["Info2"].ToString();
            myAccount.Info3 = myDataTable.Rows[0]["Info3"].ToString();
            myAccount.Info4 = myDataTable.Rows[0]["Info4"].ToString();
            myAccount.Info5 = myDataTable.Rows[0]["Info5"].ToString();
            myAccount.Info6 = myDataTable.Rows[0]["Info6"].ToString();
            myAccount.Info7 = myDataTable.Rows[0]["Info7"].ToString();
            myAccount.Info8 = myDataTable.Rows[0]["Info8"].ToString();
            myAccount.Info9 = myDataTable.Rows[0]["Info9"].ToString();
            myAccount.Info10 = myDataTable.Rows[0]["Info10"].ToString();

            if (myDataTable.Rows[0]["NInfo4"] != DBNull.Value)
            {
                myAccount.NInfo4 = Convert.ToInt32(myDataTable.Rows[0]["NInfo4"].ToString());
            }
            if (myDataTable.Rows[0]["NInfo5"] != DBNull.Value)
            {
                Int32 _Ninfo5 = 400;
                if (!Int32.TryParse(myDataTable.Rows[0]["NInfo5"].ToString(), out _Ninfo5))
                {
                    _Ninfo5 = 400;
                }
                myAccount.NInfo5 = _Ninfo5;
            }
            if (myDataTable.Rows[0]["NInfo6"] != DBNull.Value)
            {
                Int32 _Ninfo6 = 0;
                if (!Int32.TryParse(myDataTable.Rows[0]["NInfo6"].ToString(), out _Ninfo6))
                {
                    _Ninfo6 = 0;
                }
                myAccount.NInfo6 = _Ninfo6;
            }

            if (myDataTable.Rows[0]["DInfo1"] != DBNull.Value)
            {
                myAccount.Dinfo1 = myDataTable.Rows[0]["DInfo1"].ToString();
            }
            return myAccount;
        }

    }
}